package com.fantasy.robot;

import java.awt.AWTException;
import java.awt.Robot;
import java.awt.event.InputEvent;
import java.awt.event.KeyEvent;
import java.util.Properties;

import com.fantasy.readproperties.ReadProperties;

public class TestRobot {
	private static Robot robot = null;
	private static Properties keyValueMap = null;
	private static int keyPressSpan = 100;
	private static int keyDownSpan = 20;

	public static void main(String[] args) {
		String ss = "/";
		System.out.println(ss.charAt(0) + "====" + (byte) ss.charAt(0) + "===" + KeyEvent.VK_A);

		System.out.println("----------------------");
		TestRobot.openRobot();
		TestRobot.robot.keyPress(16);
		TestRobot.robot.keyPress(58);
		TestRobot.robot.keyRelease(58);
		TestRobot.robot.keyRelease(16);
		//TestRobot.press(")");(
	}

	/**
	 * 延迟按键机器人
	 */
	public static void delay(int ms) {
		robot.delay(ms);
	}

	/**
	 * 按下按键并松开
	 */
	public static void keyDown(int key) {
		robot.keyPress(key);
		robot.delay(keyDownSpan);
		robot.keyRelease(key);
	}
	
	/**
	 * 鼠标左键单击
	 */
	public static void mouseLeftKeyDown() {
		mouseLeftPress();
		delay(10);
		mouseLeftRelease();
	}
	
	/**
	 * 鼠标左键双击
	 */
	public static void mouseLeftDoubleKeyDown() {
		mouseLeftKeyDown();
		delay(10);
		mouseLeftKeyDown();
	}
	
	/**
	 * 鼠标右键单击
	 */
	public static void mouseRightKeyDown() {
		mouseRightPress();
		delay(10);
		mouseRightRelease();
	}

	/**
	 * 按下按键
	 */
	public static void keyPress(int key) {
		robot.keyPress(key);
	}

	/**
	 * 松开按键
	 */
	public static void keyRelease(int key) {
		robot.keyRelease(key);
	}
	
	/**
	 * 按下鼠标左键
	 */
	public static void mouseLeftPress() {
		robot.mousePress(InputEvent.BUTTON1_MASK);
	}
	
	/**
	 * 松开鼠标左键
	 */
	public static void mouseLeftRelease() {
		robot.mouseRelease(InputEvent.BUTTON1_MASK);
	}
	
	/**
	 * 按下鼠标右键
	 */
	public static void mouseRightPress() {
		robot.mousePress(InputEvent.BUTTON3_MASK);
	}
	
	/**
	 * 鼠标移动
	 */
	public static void mouseMove(int x , int y) {
		robot.mouseMove(x, y);
	}
	
	/**
	 * 鼠标左键按下位置
	 */
	public static void mouseLeftKeyDown(int x , int y) {
		mouseMove(x,y);
		mouseLeftKeyDown();
	}
	
	/**
	 * 鼠标左键按下位置
	 */
	public static void mouseLeftKeyDown(int[] location) {
		if(location == null){
			mouseLeftKeyDown();
		}else{
			mouseLeftKeyDown(location[0],location[1]);
		}
	}
	
	/**
	 * 鼠标左键双击位置
	 */
	public static void mouseLeftDoubleKeyDown(int[] location) {
		if(location == null){
			mouseLeftDoubleKeyDown();
		}else{
			mouseMove(location[0], location[1]);
			mouseLeftDoubleKeyDown();
		}
	}
	
	/**
	 * 鼠标右键点击位置
	 */
	public static void mouseRightKeyDown(int[] location) {
		if(location == null){
			mouseRightKeyDown();
		}else{
			mouseMove(location[0], location[1]);
			mouseRightKeyDown();
		}
	}
	
	/**
	 * 松开鼠标左键
	 */
	public static void mouseRightRelease() {
		robot.mouseRelease(InputEvent.BUTTON3_MASK);
	}

	/**
	 * 开启一个机器人
	 */
	public static void openRobot() {
		openRobot(keyPressSpan, keyDownSpan);
	}

	/**
	 * 开启一个机器人
	 */
	public static void openRobot(int keyPressSpanCust, int keyDownSpanCust) {
		try {
			robot = new Robot();
			keyValueMap = ReadProperties.read("KeyValueMap.properties");
			if (keyPressSpanCust > 0) {
				keyPressSpan = keyPressSpanCust;
			}
			if (keyDownSpanCust > 0) {
				keyDownSpan = keyDownSpanCust;
			}
		} catch (AWTException e) {
			e.printStackTrace();
		}
	}

	/**
	 * 解析字符串并执行
	 * */
	public static void press(String inputString) {
		for (int i = 0; i < inputString.length(); i++) {
			char string = inputString.charAt(i);
			String keyValues = "";
			// System.out.println("-----" + string + "----");
			if ("/".charAt(0) == string) {
				if ("{".equals(inputString.charAt(i + 1))) {
					i++;
					string = "{".charAt(0);
				}
			}
			if ("{".charAt(0) == string) {
				int lastkeyInx = inputString.indexOf("}", i);
				if (lastkeyInx != -1) {
					String keyStr = inputString.substring(i + 1, lastkeyInx);
					i = lastkeyInx;
					keyValues = keyValues + parseKeyLongStr(keyStr);
				}
			} else {
				keyValues = keyValueMap.getProperty(String.valueOf(string).replace(" ", "/s"));
				if (keyValues.length() == 0) {
					System.out.println("没有对应的键盘码，请补充" + string + "的键盘码");
				}
			}
			String[] keyList = null;
			keyList = keyValues.split(",");
			// System.out.println(keyList.length);
			/*
			 * for (String string2 : keyList) { System.out.println("----" +
			 * string2); }
			 */
			execKeyList(keyList);
		}
	}

	/**
	 * 执行按键数组
	 */
	private static void execKeyList(String[] keyList) {
		for (String key : keyList) {
			// System.out.println(key);
			if (key.length() == 0) {

			} else if (key.startsWith("delay")) {
				int delayInt = Integer.valueOf(key.replace("delay", ""));
				robot.delay(delayInt);
			} else if (key.startsWith("click")) {
				mouseLeftKeyDown(parseLocation(key.replace("click", "")));
			} else if (key.startsWith("dclick")) {
				mouseLeftDoubleKeyDown(parseLocation(key.replace("dclick", "")));
			} else if (key.startsWith("rclick")) {
				mouseRightKeyDown(parseLocation(key.replace("rclick", "")));
			} else if (key.startsWith("*")) {
				int keyInt = Integer.valueOf(key.replace("*", ""));
				keyDown(keyInt);
			} else {
				robot.keyPress(Integer.valueOf(key));
			}
		}
		robot.delay(keyDownSpan);
		for (int j = keyList.length - 1; j > -1; j--) {
			if (keyList[j].length() == 0) {

				// 延迟还有重复输入，不需要释放
			} else if (keyList[j].startsWith("delay") || keyList[j].startsWith("*")
					|| keyList[j].startsWith("click") || keyList[j].startsWith("dclick")
					|| keyList[j].startsWith("rclick")){

			} else {
				robot.keyRelease(Integer.valueOf(keyList[j]));
			}
		}
		robot.delay(keyPressSpan);
	}
	
	private static int[] parseLocation(String location){
		int[] res = null;
		String[] strs = location.split("#");
		if(strs.length > 0){
			res = new int[strs.length];
			for (int i = 0; i < strs.length; i++) {
				res[i] = Integer.valueOf(strs[i]);
			}
		}
		return res;
	}

	/**
	 * 解析字符串为按键列表
	 */
	private static String parseKeyLongStr(String keyStr) {
		String res = "";

		if (keyValueMap.containsKey(keyStr)) {
			res = res + "," + keyValueMap.getProperty(keyStr);
		} else if (keyStr.toLowerCase().startsWith("delay")) {
			res = res + "," + keyStr.toLowerCase();
		} else if(keyStr.toLowerCase().startsWith("click")){
			res = res + "," + keyStr.toLowerCase();
		} else if(keyStr.toLowerCase().startsWith("dclick")){
			res = res + "," + keyStr.toLowerCase();
		} else if(keyStr.toLowerCase().startsWith("rclick")){
			res = res + "," + keyStr.toLowerCase();
		} else if (keyStr.contains("*")) {
			int inx = keyStr.indexOf("*");
			String key = keyStr.substring(0, inx);
			String keyValue = keyValueMap.getProperty(key);
			if (keyValue == null) {
				System.out.println("没有对应的键盘码，请补充" + key + "的键盘码");
			} else {
				int repeatQty = Integer.valueOf(keyStr.substring(inx + 1));
				for (int i = 0; i < repeatQty; i++) {
					res = res + ",*" + keyValue;
				}
			}
		} else {
			for (int keyInx = 0; keyInx < keyStr.length(); keyInx++) {
				String keyValue = keyValueMap.getProperty(String.valueOf(keyStr.charAt(keyInx)).replace(" ", "/s"));
				if (keyValue == null) {
					System.out.println("没有对应的键盘码，请补充" + String.valueOf(keyStr.charAt(keyInx)) + "的键盘码");
				} else {
					res = res + "," + keyValue;
				}
			}
		}

		return res;
	}

	public static void todelete() {
		robot.delay(2000);
		robot.keyPress(KeyEvent.VK_DOWN);
		robot.keyRelease(KeyEvent.VK_DOWN);
		robot.delay(500);
		robot.keyPress(KeyEvent.VK_ENTER);
		robot.keyRelease(KeyEvent.VK_ENTER);
		robot.delay(500);
		robot.keyPress(KeyEvent.VK_ENTER);
		robot.keyRelease(KeyEvent.VK_ENTER);
		robot.delay(500);
		robot.keyPress(KeyEvent.VK_ENTER);
		robot.keyRelease(KeyEvent.VK_ENTER);
		robot.delay(500);
		robot.keyPress(KeyEvent.VK_ENTER);
		robot.keyRelease(KeyEvent.VK_ENTER);
		robot.delay(500);
		robot.keyPress(KeyEvent.VK_ENTER);
		robot.keyRelease(KeyEvent.VK_ENTER);
		robot.delay(500);
		robot.keyPress(KeyEvent.VK_CONTROL);
		robot.keyPress(KeyEvent.VK_D);
		robot.keyRelease(KeyEvent.VK_CONTROL);
		robot.keyRelease(KeyEvent.VK_D);
		robot.delay(500);
		robot.keyPress(KeyEvent.VK_ENTER);
		robot.keyRelease(KeyEvent.VK_ENTER);
		robot.delay(500);
		robot.keyPress(KeyEvent.VK_ENTER);
		robot.keyRelease(KeyEvent.VK_ENTER);
		robot.delay(500);
	}
}
